/*
 * Copyright 2008 Wilfred Springer
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.agilejava.javaone.tools;

import java.io.PrintStream;
import java.util.List;

import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import org.xml.sax.ErrorHandler;

import com.agilejava.javaone.Logger;
import com.agilejava.javaone.ScheduleBuilderSessionCatalog;
import com.agilejava.javaone.Session;
import com.agilejava.javaone.SessionCatalog;

public abstract class AbstractJavaOneTool {

    @Option(name = "-u", usage = "Your JavaOne registration username", required = true)
    private String username;

    @Option(name = "-p", usage = "Your JavaOne registration password", required = true)
    private String password;

    protected String getUsername() {
        return username;
    }

    protected String getPassword() {
        return password;
    }

    public static void execute(AbstractJavaOneTool tool, String[] args) {
        CmdLineParser cmdline = new CmdLineParser(tool);
        try {
            cmdline.parseArgument(args);
        } catch (CmdLineException e) {
            error(cmdline, e.getMessage(), tool.getName());
        }
        tool.execute(new ConsoleErrorHandler(cmdline, tool.getName()));
        System.exit(0);
    }

    private static void error(CmdLineParser cmdline, String message, String name) {
        System.err.println(message);
        System.err.println();
        printUsage(cmdline, System.err, name);
        System.exit(1);
    }

    private static void printUsage(CmdLineParser cmdline, PrintStream out,
            String name) {
        out.println("Usage: " + name + " [OPTIONS]");
        out.println();
        cmdline.printUsage(out);
    }

    public abstract void execute(ErrorHandler handler);

    public abstract String getName();

    public abstract SessionCatalog getCatalog();

    protected List<? extends Session> getSessions() {
        SessionCatalog catalog = getCatalog();
        catalog.setLogger(new ConsoleLogger());
        List<? extends Session> sessions = catalog.getSessions();
        return sessions;
    }

    private static class ConsoleLogger implements Logger {

        public void logLoadingPresenter(String id) {
            System.out.println("Loading presenter " + id);
        }

        public void logSessionCount(int sessions) {
            System.out.println("Loading " + sessions + " sessions");
        }

        public void logLoadingSession(String id, int number, int total) {
            System.out.println("Loading session " + id + " (" + number + " of "
                    + total + ")");
        }

        public void logFailedDownloadingSessionDetails(String id) {
            System.out
                    .println("Failed to download session details for session "
                            + id);
        }

    }

    public interface ErrorHandler {

        void error(String message);

    }

    public static class ConsoleErrorHandler implements ErrorHandler {

        private CmdLineParser cmdline;

        private String name;

        public ConsoleErrorHandler(CmdLineParser cmdline, String name) {
            this.cmdline = cmdline;
            this.name = name;
        }

        public void error(String message) {
            AbstractJavaOneTool.error(cmdline, message, name);
        }

    }

}
